import axios from 'axios'

const api = axios.create({
  baseURL: import.meta.env.VITE_APP_BASE_API,
  timeout: 5000
})

export const shopApi = {
  getShops() {
    return api.get('/shops')
  },
  
  createShop(data) {
    return api.post('/shops', data)
  },
  
  updateShop(id, data) {
    return api.put(`/shops/${id}`, data)
  },
  
  deleteShop(id) {
    return api.delete(`/shops/${id}`)
  },
  
  getShopDetails(id) {
    return api.get(`/shops/${id}`)
  }
}

// 请求拦截器
api.interceptors.request.use(
  config => {
    const token = localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
api.interceptors.response.use(
  response => response,
  error => {
    if (error.response?.status === 401) {
      // 处理未授权的情况
      localStorage.removeItem('token')
      window.location.href = '/login'
    }
    return Promise.reject(error)
  }
)

export default api 